package bw.owl;

import com.clarkparsia.modularity.IncrementalClassifier;
import com.clarkparsia.pellet.owlapiv3.PelletReasoner;

import java.io.File;
import java.util.ArrayList;
import java.util.Set;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.io.RDFXMLOntologyFormat;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLNamedIndividual;
import org.semanticweb.owlapi.model.OWLObjectProperty;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyManager;

public class Ontology {

    private static OWLOntologyManager manager;
    private static OWLDataFactory factory;
    private static OWLOntology ontology;
    private static IncrementalClassifier classifyer;
    private static PelletReasoner reasoner;
    private static String owlString = "http://www.semanticweb.org/ontologies/2011/3/Ontology1302512911713.owl";

    private static OWLClass pracownik;
    private static OWLClass zadanie;
    private static OWLClass umiejetnosc;

    private static ArrayList<Pracownik> pracownicy;
    private static ArrayList<Umiejetnosc> umiejetnosci;
    private static ArrayList<Zadanie> zadania;

    public static void load(File file) {

        try {
            manager = OWLManager.createOWLOntologyManager();
            factory = manager.getOWLDataFactory();
            ontology = manager.loadOntologyFromOntologyDocument(file);
            classifyer = new IncrementalClassifier(ontology);
            reasoner = classifyer.getReasoner();

        } catch (Exception e) {
            e.printStackTrace();
        }
        reload();
    }

    public static void reload() {
        classifyer.classify();
        pracownik = factory.getOWLClass(IRI.create(owlString + "#Pracownik"));
        zadanie = factory.getOWLClass(IRI.create(owlString + "#Zadanie"));
        umiejetnosc = factory.getOWLClass(IRI.create(owlString + "#Umiejetnosc"));

        pracownicy = new ArrayList<Pracownik>();
        umiejetnosci = new ArrayList<Umiejetnosc>();
        zadania = new ArrayList<Zadanie>();

        Set<OWLNamedIndividual> zestawPracownikow = reasoner.getInstances(pracownik, false).getFlattened();
            for (OWLNamedIndividual ind : zestawPracownikow) {
                //System.out.println("Pracownicy: " + ind.toString());

                Pracownik l = new Pracownik(ind, ind.toString());

                pracownicy.add(l);
            }

        Set<OWLNamedIndividual> zestawUmiejetnosci = reasoner.getInstances(umiejetnosc, false).getFlattened();
            for (OWLNamedIndividual ind : zestawUmiejetnosci) {
                //System.out.println("Pracownicy: " + ind.toString());

                Umiejetnosc l = new Umiejetnosc(ind, ind.toString());

                umiejetnosci.add(l);
            }

        Set<OWLNamedIndividual> zestawZadan = reasoner.getInstances(zadanie, false).getFlattened();
            for (OWLNamedIndividual ind : zestawZadan) {
                //System.out.println("Pracownicy: " + ind.toString());

                Zadanie l = new Zadanie(ind, ind.toString());

                zadania.add(l);
            }
    }


    

    public static ArrayList<Pracownik> getPracownicy() {
        return pracownicy;
    }

    public static ArrayList<Umiejetnosc> getUmiejetnosci() {
        return umiejetnosci;
    }

    public static ArrayList<Zadanie> getZadania() {
        return zadania;
    }

    public static OWLDataFactory getFactory() {
        return factory;
    }

    public static String getOwlString() {
        return owlString;
    }

    public static OWLOntology getOntology() {
        return ontology;
    }

    public static OWLOntologyManager getManager() {
        return manager;
    }
    
    public static PelletReasoner getReasoner() {
	return reasoner;
    }



}
